/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools mkfs super block writer
 */

#ifndef SUPER_BLOCK_WRITER_H
#define SUPER_BLOCK_WRITER_H

#include <memory>
#include <string>
#include <sys/types.h>

#include "hmfs_command.h"
#include "hmfs_fs.h"

namespace OHOS {
namespace Hmfs {
union SuperBlockOnDisk {
    struct {
        uint8_t empty[HMFS_SUPER_BLOCK_OFFSET];
        SuperBlockData superBlock;
    };
    uint8_t size[HMFS_BLOCK_SIZE];
};

class MkfsFormat;
class SuperBlockWriter {
public:
    SuperBlockWriter(MkfsFormat &mkfs);
    ~SuperBlockWriter() = default;

    int32_t Format();

private:
    friend class MkfsFormat;

    int32_t Write();
    int32_t InitBasicInfo();
    int32_t ClacCheckSum();
    int32_t FillSuperBlockData();
    int32_t FillSuperBlockDataEnd(SuperBlockData *superBlock);
    void FillExtList();
    bool IsExtensionDuplicate(std::string &ext);
    int32_t UpdateHmfsData();

    MkfsFormat &mkfs_;
    std::unique_ptr<SuperBlockOnDisk> superBlock_ = nullptr;
};

} // namespace Hmfs
} // namespace OHOS
#endif // SUPER_BLOCK_WRITER_H
